/**
 * 92. 背包问题
 * 在n个物品中挑选若干物品装入背包，最多能装多满？假设背包的大小为m，每个物品的大小为A[i]
 * https://www.lintcode.com/problem/backpack/description
 *
 * 样例
 * 样例 1:
 * 	输入:  [3,4,8,5], backpack size=10
 * 	输出:  9
 *
 * 样例 2:
 * 	输入:  [2,3,5,7], backpack size=12
 * 	输出:  12
 *
 * 挑战
 * O(n x m) time and O(m) memory.
 *
 * O(n x m) memory is also acceptable if you do not know how to optimize memory.
 *
 * 注意事项
 * 你不可以将物品进行切割。
 * @author jeymingwu
 * @date 2019/9/29 16:14
 */
public class L0092_Backpack {
    /**
     * 方法一：转化为0-1背包问题
     * @param m: An integer m denotes the size of a backpack
     * @param A: Given n items with size A[i]
     * @return: The maximum size
     */
    public int backPack(int m, int[] A) {
        // write your code here// 方法1：转化为0-1背包问题
        int[][] dp = new int[A.length + 1][m + 1];

        for (int i = 0; i <= A.length; ++i) {
            for (int j = 0; j <= m; ++j) {
                if (i == 0 || j == 0) {
                    dp[i][j] = 0;
                } else if (A[i - 1] > j) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.max(dp[i -1][j], dp[i - 1][j - A[i - 1]] + A[i - 1]);
                }
            }
        }
        return dp[A.length][m];
    }

    /**
     * 方法二：计算前i件物品能否装下
     * @param m: An integer m denotes the size of a backpack
     * @param A: Given n items with size A[i]
     * @return: The maximum size
     */
    public int backPack2(int m, int[] A) {
        boolean[][] dp = new boolean[A.length + 1][m + 1];

        for (int i = 0; i <= A.length; ++i) {
            for (int j = 0; j <= m; ++j) {
                dp[i][j] = false;
            }
        }

        dp[0][0] = true;

        for (int i = 1; i <= A.length; ++i) {
            for (int j = 0; j <= m; ++j) {
                dp[i][j] = dp[i - 1][j];
                if (j >= A[i - 1] && dp[i - 1][j - A[i - 1]]) {
                    dp[i][j] = true;
                }
            }
        }

        for (int i = m; i >= 0; --i) {
            if (dp[A.length][i]) {
                return i;
            }
        }

        return 0;
    }

    public int backPack3(int m, int[] A) {
        boolean[][] dp = new boolean[A.length + 1][m + 1];
        for (int i = 0; i <= A.length; ++i) {
            for (int j = 0; j <= m; ++j) {
                dp[i][j] = false;
                if (j == 0) {
                    dp[i][j] = true;
                }
                if (i - 1 >= 0 && ((dp[i - 1][j]) || ((j - A[i - 1] >= 0) && (dp[i - 1][j - A[i - 1]])))) {
                    dp[i][j] = true;
                }
            }
        }

        for (int i = 0; i <= A.length; ++i) {
            for (int j = 0; j <= m; ++j) {
                System.out.print(dp[i][j] == true ? 1 : 0);
            }
            System.out.println();
        }

        for (int i = m; i >= 0; --i) {
            if (dp[A.length][i]) {
                return i;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        int[] nums = {3, 4, 8, 5};
        L0092_Backpack backpack0092 = new L0092_Backpack();
        System.out.println(backpack0092.backPack3(10, nums));
    }
}
